package com.itheima.reggie.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author kcs
 * @date 2021/11/24
 * @TIME:16:20
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     *
     * @param setmealDto
     */
    @Override
    public void add(SetmealDto setmealDto) {

        //1.添加套餐表数据
        this.save(setmealDto);

        //2.设置套餐id
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((c) -> {
            c.setSetmealId(setmealDto.getId());
            return c;
        }).collect(Collectors.toList());

        //3.写入数据库
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 套餐信息分页查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public IPage<SetmealDto> findByPage(Integer page, Integer pageSize, String name) {

        //设置分页条件
        IPage<Setmeal> iPage = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        if (name != null && name != "") {
            wrapper.like(Setmeal::getName, name);
        }
        wrapper.eq(Setmeal::getIsDeleted, 0);

        //执行查询操作
        IPage<Setmeal> pageInfo = this.page(iPage, wrapper);

        //设置categoryName
        List<Setmeal> setmeals = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = JSON.parseArray(JSON.toJSONString(setmeals), SetmealDto.class);
        setmealDtos = setmealDtos.stream().map(c -> {
            Category category = categoryService.getById(c.getCategoryId());
            c.setCategoryName(category.getName());
            return c;
        }).collect(Collectors.toList());

        IPage<SetmealDto> dtoIPage = new Page<>();
        dtoIPage.setRecords(setmealDtos);
        dtoIPage.setTotal(pageInfo.getTotal());
        return dtoIPage;
    }

    /**
     * 套餐管理 -- 批量起售/批量停售
     *
     * @param status
     * @param ids
     */
    @Override
    public void updateStatus(Integer status, Long[] ids) {

        //1.查询出需要操作的套餐
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));

        //2.设置状态
        setmeals = setmeals.stream().map(c -> {
            c.setStatus(status);
            return c;
        }).collect(Collectors.toList());

        //3.进行批量修改
        this.updateBatchById(setmeals);
    }

    /**
     * 套餐管理 -- 批量删除(逻辑删除)
     *
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {

        //1.逻辑删除套餐表
        //查询出要删除的套餐
        List<Setmeal> setmeals = this.listByIds(Arrays.asList(ids));

        //1.2设置is_deleted字段为1
        setmeals = setmeals.stream().map(c -> {
            c.setIsDeleted(1);
            return c;
        }).collect(Collectors.toList());

        //1.3执行批量修改操作
        this.updateBatchById(setmeals);

        //2.逻辑删除(修改)套餐菜品信息
        for (Long id : ids) {

            //查询出套餐的菜品信息
            LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SetmealDish::getSetmealId, id);
            List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

            //设置is_deleted字段
            setmealDishes = setmealDishes.stream().map(c -> {
                c.setIsDeleted(1);
                return c;
            }).collect(Collectors.toList());

            //更新套餐菜品数据
            setmealDishService.updateBatchById(setmealDishes);
        }
    }

    /**
     * 套餐管理 -- 修改套餐(回显)
     *
     * @return
     */
    @Override
    public R findById(Long id) {

        //数据校验

        //1.根据id查询套餐信息
        Setmeal setmeal = this.getById(id);

        //2.查询套餐对应的菜品信息
        LambdaUpdateWrapper<SetmealDish> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, id).eq(SetmealDish::getIsDeleted, 0);
        List<SetmealDish> setmealDishes = setmealDishService.list(wrapper);

        //3.设置categoryName
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        Category category = categoryService.getById(setmeal.getCategoryId());
        setmealDto.setCategoryName(category.getName());
        setmealDto.setSetmealDishes(setmealDishes);

        return R.success(setmealDto);
    }

    /**
     * 套餐管理 -- 修改套餐(修改)
     *
     * @param setmealDto
     */
    @Override
    public void updateSetmeal(SetmealDto setmealDto) {

        //1.修改套餐数据
        this.updateById(setmealDto);

        //2.修改套餐菜品数据,先删除,再添加
        //先删除
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId, setmealDto.getId());
        setmealDishService.remove(wrapper);

        //再添加
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map(c -> {
            c.setSetmealId(setmealDto.getId());
            return c;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 根据分类ID查询套餐(前台菜品展示)
     *
     * @param setmeal
     * @return
     */
    @Override
    public List<Setmeal> listSetmeal(Setmeal setmeal) {

        //设置查询套餐条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null, Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null,Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        //查询套餐
        List<Setmeal> setmeals = this.list(wrapper);
        return setmeals;
    }
}
